package cn.community.system.web.controller;

import cn.community.system.util.UploadUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@Controller
public class UploadController {

    // OSS图片上传，uploadfive
    @RequestMapping("/uploadImg")
    @ResponseBody
    public Object ossUploadImg(MultipartFile pic){
        String url = null;
        try {
            url = UploadUtil.uploadAli(pic);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return url;
    }

    // 富文本编辑器上传(CKEditor  按照插件要求返回结果弄，返回类型为 Map<String, Object>)
  /*  成功：
        {
        "uploaded": 1,
            "fileName": "foo.jpg",
            "url": "/files/foo.jpg"
    }*/
    /* 失败;{
    "uploaded": 0,
    "error": {
        "message": "The file is too big."
    }
    }*/
    @RequestMapping("/uploadImg_ck")
    @ResponseBody
    public Map<String, Object> ckEditorUploadImg(MultipartFile upload){
        Map<String, Object> map = new HashMap<>();
        String url = null;
        // uploaded不能为空且有值(有内容或大小)才上传
        if(upload != null && upload.getSize() > 0){
            try {
                url = UploadUtil.uploadAli(upload);
                map.put("uploaded",1);
                map.put("url",url);
            } catch (Exception e) {
                e.printStackTrace();
                map.put("uploaded",0);
                Map<String, Object> mm = new HashMap<>();
                mm.put("message",e.getMessage());
                map.put("error",mm);
            }
        }
        return map;
    }
}
